package com.itheima.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateConverterUtils {
    /**
     * @param dateStr  转换的时间字符串
     * @param timeZone 设置时区,如北京时区，"GMT+8"
     * @return
     * @throws ParseException
     */
    public static Date String2Date(String dateStr, String timeZone) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        return simpleDateFormat.parse(dateStr);
    }

    /**
     * @param date     转换的时间字符串
     * @param timeZone 设置时区,如北京时区，"GMT+8"
     * @return
     * @throws ParseException
     */
    public static String Date2String(Date date, String timeZone) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        return simpleDateFormat.format(date);
    }

    public static void main(String[] args) throws ParseException {
        Date date = String2Date("2020-01-02", "GMT+8");
    }
}
